package com.exam.project.business.service.impl;

import java.util.List;

import com.exam.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.exam.project.business.mapper.ExamStudentMapper;
import com.exam.project.business.domain.ExamStudent;
import com.exam.project.business.service.IExamStudentService;

/**
 * 考试关联用户表Service业务层处理
 *
 * @author qiuwenbing
 * @date 2025-04-03
 */
@Service
public class ExamStudentServiceImpl implements IExamStudentService {
    @Autowired
    private ExamStudentMapper examStudentMapper;

    /**
     * 查询考试关联用户表
     *
     * @param id 考试关联用户表主键
     * @return 考试关联用户表
     */
    @Override
    public ExamStudent selectExamStudentById(Long id) {
        return examStudentMapper.selectExamStudentById(id);
    }

    /**
     * 查询考试关联用户表列表
     *
     * @param examStudent 考试关联用户表
     * @return 考试关联用户表
     */
    @Override
    public List<ExamStudent> selectExamStudentList(ExamStudent examStudent) {
        return examStudentMapper.selectExamStudentList(examStudent);
    }

    /**
     * 新增考试关联用户表
     *
     * @param examStudent 考试关联用户表
     * @return 结果
     */
    @Override
    public int insertExamStudent(ExamStudent examStudent) {
        // 先删除
        examStudentMapper.deleteExamStudentByExamId(examStudent.getExamId());
        examStudent.getUserIds().forEach(userId -> {
            ExamStudent insert = new ExamStudent();
            insert.setCreateTime(DateUtils.getNowDate());
            insert.setExamId(examStudent.getExamId());
            insert.setUserId(userId);
            examStudentMapper.insertExamStudent(insert);
        });
        return 1;
    }

    /**
     * 修改考试关联用户表
     *
     * @param examStudent 考试关联用户表
     * @return 结果
     */
    @Override
    public int updateExamStudent(ExamStudent examStudent) {
        examStudent.setUpdateTime(DateUtils.getNowDate());
        return examStudentMapper.updateExamStudent(examStudent);
    }

    /**
     * 批量删除考试关联用户表
     *
     * @param ids 需要删除的考试关联用户表主键
     * @return 结果
     */
    @Override
    public int deleteExamStudentByIds(Long[] ids) {
        return examStudentMapper.deleteExamStudentByIds(ids);
    }

    /**
     * 删除考试关联用户表信息
     *
     * @param id 考试关联用户表主键
     * @return 结果
     */
    @Override
    public int deleteExamStudentById(Long id) {
        return examStudentMapper.deleteExamStudentById(id);
    }
}
